package org.afun.csms_android.adapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.afun.csms_android.R;
import org.afun.csms_android.domain.Course;
import org.afun.csms_android.domain.Teacher;
import org.afun.csms_android.utils.MyTableOperator;
import org.afun.csms_android.utils.TableOperator;

import java.util.List;

public class Teacher2CourseSelectAdapter extends RecyclerView.Adapter<Teacher2CourseSelectAdapter.ViewHolder> {
    private List<Course> mCourseList;
    private Context mContext;
    private Teacher teacher;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View SelectCourseView;
        ImageView courseImage;
        TextView courseMsg;
        Button selectBtn;

        public ViewHolder(View view) {
            super(view);
            SelectCourseView = view;
            courseImage = (ImageView) view.findViewById(R.id.selectcourse_image);
            courseMsg = view.findViewById(R.id.selectcourse_msg);
            selectBtn = view.findViewById(R.id.selectBtn);
        }

    }

    public Teacher2CourseSelectAdapter(List<Course> courseList, Context context, Teacher teacher) {
        this.mCourseList = courseList;
        mContext = context;
        this.teacher = teacher;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.teacherselectcourseitem, parent, false);
        final ViewHolder holder = new ViewHolder(view);

        holder.selectBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final int position = holder.getAdapterPosition();
                Course course = mCourseList.get(position);
                new AlertDialog.Builder(mContext).setTitle("提示")
                        .setMessage("你是否确定选择为" + course.getCouId() + "的课程？")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                            @SuppressLint("NotifyDataSetChanged")
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                course.setIsAudit(2);
                                MyTableOperator myTableOperator = new MyTableOperator(mContext);
                                TableOperator tableOperator = myTableOperator.getTableOperator();
                                boolean b = tableOperator.teacherSelectCourse(course, teacher);
                                if (b) {
                                    new androidx.appcompat.app.AlertDialog.Builder(mContext)
                                            .setTitle("提示信息")
                                            .setMessage("选课成功！")
                                            .setPositiveButton("确定", null)
                                            .create()
                                            .show();
                                    mCourseList.remove(position);
                                    notifyItemRemoved(position);
                                    notifyItemRangeChanged(position, mCourseList.size());
                                } else {
                                    new androidx.appcompat.app.AlertDialog.Builder(mContext)
                                            .setTitle("提示信息")
                                            .setMessage("选课失败...")
                                            .setPositiveButton("确定", null)
                                            .create()
                                            .show();
                                }

                            }
                        })
                        .setNegativeButton("否", null)
                        .show();
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Course course = mCourseList.get(position);
        holder.courseImage.setImageResource(R.drawable.img);
        int couId = course.getCouId();
        String couName = course.getCouName();
        int credit = course.getCredit();

        holder.courseMsg.setText(
                "课程号：" + couId + "\n课程名：" + couName + "\n学分：" + credit
        );


    }


    @Override
    public int getItemCount() {
        return mCourseList.size();
    }
}
